package com.ktjy.crm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.crm.entity.SysUser;
import com.ktjy.crm.service.impl.SysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SysUserController {

    @Resource
    SysUserService sysUserService;

    @RequestMapping("/login")
    public String login() {
        return "/login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(Model model, SysUser sysUser, HttpSession session) {
        SysUser user = null;
        if (sysUser.getAccount().length() < 1 || sysUser.getAccount() == null) {
            session.setAttribute("user", null);
            return "error";
        } else {
            user = sysUserService.login(sysUser.getAccount(), sysUser.getPassword());
            if (user == null) {
                session.setAttribute("user", null);
                return "error";
            } else if (!user.getPassword().equals(sysUser.getPassword())) {
                session.setAttribute("user", null);
                return "error";
            } else {
                session.setAttribute("user", user);
                return page(model, 1, 5);
            }
        }

    }

    @RequestMapping("/page")
    public String page(Model model,
                       @RequestParam(value = "current", defaultValue = "1") Integer current,
                       @RequestParam(value = "size", defaultValue = "5") Integer size) {  // 修改这里的默认值为5
        Page page = sysUserService.selectPage(current, size);
        model.addAttribute("page", page);
        return "Page";
    }

    @RequestMapping("/selectById")
    public String selectById(Model model, Integer id) {
        SysUser sysUser = sysUserService.selectById(id);
        model.addAttribute("sysUser", sysUser);
        return "update";
    }

    @RequestMapping("/deleteById")
    public String deleteById(Model model, Integer id) {
        int i = sysUserService.deleteById(id);
        return page(model, 1, 5);
    }

    @RequestMapping("/updateById")
    public String updateById(Model model, SysUser sysUser) {
        int i = sysUserService.updateById(sysUser);
        return page(model, 1, 5);
    }

    @RequestMapping("/add")
    public String add() {
        return "/insert";
    }

    @RequestMapping("/insert")
    public String insert(Model model, SysUser sysUser) {
        int i = sysUserService.insert(sysUser);
        return page(model, 1, 5);
    }

    @RequestMapping("/selectByName")
    public String selectByName(Model model,
                               @RequestParam(value = "realName", required = false) String realName,
                               @RequestParam(value = "current", defaultValue = "1") Integer current,
                               @RequestParam(value = "size", defaultValue = "5") Integer size) {  // 这里也改为5
        if (realName == null || realName.trim().isEmpty()) {
            return page(model, current, size);
        }

        Page<SysUser> page = sysUserService.selectPageByName(realName, current, size);
        model.addAttribute("page", page);
        model.addAttribute("searchKeyword", realName);
        return "Page";
    }
}